package com.example.teesystem.common.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.example.teesystem.filter.MyHandlerInterceptor;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 配置 mp 的自动填充功能
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "registerTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "uploadTime", Date.class, new Date());

        try {
            this.strictInsertFill(metaObject, "uploadId", Integer.class, MyHandlerInterceptor.getUid());
            this.strictInsertFill(metaObject, "updateId", Integer.class, MyHandlerInterceptor.getUid());
            this.strictInsertFill(metaObject, "teacherId", Integer.class, MyHandlerInterceptor.getUid());
        } catch (Exception ignored){}
    }
    
    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "updateId", Integer.class, MyHandlerInterceptor.getUid());
    }
}
